package com.ohos.carbon.slice.animations;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * Created by Jack on 2015/10/19.
 */
public class BallScaleIndicator extends AvLoadingIndicatorView {

    float scale = 1f;
    float alpha = 1.0f;

    public BallScaleIndicator(Context context) {
        super(context);
        DrawTask task = (component, canvas) -> {
            setPaint();
            setBound();
            draw(canvas, getPaint());
        };
        addDrawTask(task);
    }

    @Override
    public void addDrawTask(DrawTask drawTask) {
        super.addDrawTask(drawTask);
        drawTask.onDraw(this, mCanvasForTaskOverContent);
    }

    @Override
    public void draw(Canvas canvas, Paint paint) {
        float circleSpacing = 4;
        paint.setAlpha(alpha);
        canvas.scale(scale, scale, getWidth() / 2, getHeight() / 2);
        paint.setAlpha(alpha);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, 250, paint);
    }

    @Override
    public ArrayList<AnimatorValue> onCreateAnimators() {
        ArrayList<AnimatorValue> animators = new ArrayList<>();
        AnimatorValue scaleAnim = new AnimatorValue();
        scaleAnim.setCurveType(Animator.CurveType.LINEAR);
        scaleAnim.setDuration(1000);
        scaleAnim.setLoopedCount(0);
        addUpdateListener(scaleAnim, new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                scale = v;
                invalidate();
            }
        });
        AnimatorValue alphaAnim = new AnimatorValue();
        alphaAnim.setCurveType(Animator.CurveType.LINEAR);
        alphaAnim.setDuration(1000);
        alphaAnim.setLoopedCount(0);
        addUpdateListener(alphaAnim, new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                alpha = 1 - v;
                invalidate();
            }
        });
        animators.add(scaleAnim);
        animators.add(alphaAnim);
        return animators;
    }
}
